#include <REGX52.H>
#include "Key.h"
#include "Delay.h"
#include "Nixie.h"
#include "Timer0.h"
#include "MatrixKey.h"

sbit BUZZER=P2^5;

unsigned int counter,second,minute;
unsigned char second1,second2,minute1,minute2,point,m,k,keym;

void main()
{
    Timer0_Init();
    TR0=0;
	while(1)
	{    
        BUZZER=0;
        k=MatrixKey();
        if(k==1)
        {
            minute++;
        }
        if(k==2)
        {
            minute--;
        }
        if(k==3)
        {
            second++;
        }
        if(k==4)
        {
            second--;
        }
        keym=Key();
        if(keym==1)
        {
            point++;
            point%=2;
            TR0=point;
            m=2;
        }
        if(keym==2)
        {
            point++;
            point%=2;
            TR0=point;
            m=0;
        }
        if(minute==0&&second==-1)
        {TR0=0;while(1){keym=Key();if(keym==3)break;}second=0;minute=0;point=0;}
        if(second==-1)
        {
            second=59;
            minute--;
        }
        if(second==60)
        {
            minute++;
        }
        second%=60;
        second1=second/10;
        second2=second%10;
        minute1=minute/10;
        minute2=minute%10;
        nixie(1,minute1);
        nixie(2,minute2);
        nixie(3,second1);
        nixie(4,second2);
	}
}

void Timer0_Routine() interrupt 1
{
    
    TL0 = 0x18; //设置定时初值
    TH0 = 0xFC; //设置定时初值
    counter++;
    counter%=100;//时间待调
    if(counter==99)
    {second+=m-1;}
}
